package com.xuecheng.content.application.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.application.service.CoursePublishApplication;
import com.xuecheng.content.domain.model.entity.CourseBase;
import com.xuecheng.content.domain.model.entity.CoursePublish;
import com.xuecheng.content.domain.model.entity.CoursePublishPre;
import com.xuecheng.content.domain.model.enums.CourseAuditEnum;
import com.xuecheng.content.domain.model.enums.CoursePublishEnum;
import com.xuecheng.content.domain.service.CourseBaseService;
import com.xuecheng.content.domain.service.CoursePublishPreService;
import com.xuecheng.content.domain.service.CoursePublishService;
import com.xuecheng.messagesdk.model.MqMessageDo;
import com.xuecheng.messagesdk.model.enums.MqMessageTypeEnum;
import com.xuecheng.messagesdk.model.po.MqMessage;
import com.xuecheng.messagesdk.service.MqMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-08-25  15:33
 */
@Service
public class CoursePublishApplicationImpl implements CoursePublishApplication {

    @Autowired
    private CoursePublishService coursePublishService;

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private CoursePublishPreService coursePublishPreService;

    @Autowired
    private MqMessageService mqMessageService;

    @Override
    @Transactional
    public void coursePublish(Long courseId, Long companyId) {
        if (Objects.isNull(courseId)){
            throw new XueChengPlusException("课程ID不能为空");
        }
        CourseBase courseBase = courseBaseService.getById(courseId);
        if (Objects.isNull(courseBase)){
            throw new XueChengPlusException("课程不存在");
        } else if (!courseBase.getCompanyId().equals(companyId)){
            throw new XueChengPlusException("权限不足");
        } else if (!CourseAuditEnum.OK.getCode().equals(courseBase.getAuditStatus())){
            throw new XueChengPlusException("课程审核没通过，不能发布");
        } else if (CoursePublishEnum.UP.getCode().equals(courseBase.getStatus())){
            throw new XueChengPlusException("课程已发布");
        }

        // 保存课程发布信息
        saveCoursePublish(courseId);
        // 保存消息表
        saveCoursePublishMessage(courseId);
        // 删除课程预发布表中的的记录
        coursePublishPreService.removeById(courseId);
        // 更新课程状态为发布
        courseBase.setStatus(CoursePublishEnum.UP.getCode());
        courseBaseService.updateById(courseBase);

    }

    private void saveCoursePublishMessage(Long courseId) {
        if (Objects.isNull(courseId)){
            throw new XueChengPlusException("课程ID不能为空");
        }
        // 保存到消息SDK
        mqMessageService.saveMessage(new MqMessageDo(MqMessageTypeEnum.COURSE_PUSH.getType(), courseId.toString(), null, null));

    }

    private void saveCoursePublish(Long courseId) {
        if (Objects.isNull(courseId)){
            throw new XueChengPlusException("课程ID不能为空");
        }
        CoursePublishPre coursePublishPre = coursePublishPreService.getById(courseId);
        if (Objects.isNull(coursePublishPre)){
            throw new XueChengPlusException("请先提交审核后发布");
        }

        CoursePublish coursePublish = coursePublishService.getById(courseId);
        if (Objects.isNull(coursePublish)){
            coursePublish = BeanUtil.copyProperties(coursePublishPre, CoursePublish.class);
            coursePublish.setStatus(CoursePublishEnum.UP.getCode());
            coursePublishService.save(coursePublish);
        } else {
            BeanUtils.copyProperties(coursePublishPre, coursePublish);
            coursePublish.setStatus(CoursePublishEnum.UP.getCode());
            coursePublishService.updateById(coursePublish);
        }
    }

    @Override
    public CoursePublish getCoursePublish(Long courseId) {
        return coursePublishService.getById(courseId);
    }
}
